package com.wontube.client.pub;

import java.io.IOException;
import java.io.Serializable;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import com.wontube.client.json.JsonReader;
import com.wontube.client.json.JsonWriter;


/**
 * @author yuechao
 * 
 */
public final class ProxyRequest implements Serializable {
	private static final long serialVersionUID = 7817833355319961576L;

	private RequestHeaders headers;
	private String[] arguments;

	public RequestHeaders getHeaders() {
		return this.headers;
	}

	public void setHeaders(RequestHeaders headers) {
		this.headers = headers;
	}

	public String[] getArguments() {
		return this.arguments;
	}

	public void setArguments(String[] arguments) {
		this.arguments = arguments;
	}
	
	public String toJsonString() {
		StringWriter sw = new StringWriter();
		JsonWriter jsonWriter = new JsonWriter(sw);
		try {
			this.write(jsonWriter);
			jsonWriter.flush();
			return sw.toString();
		} catch (IOException e) {
		} finally {
			try {
				jsonWriter.close();
			} catch (IOException e) {
			}
		}
		
		return "";
	}
	
	public ProxyRequest fromJosnString(String json) {
		JsonReader reader = new JsonReader(new StringReader(json));
		reader.setLenient(true);
		try {
			this.read(reader);
		} catch (IOException e) {
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
			}
		}
		
		return this;
	}
	
	public void write(JsonWriter writer) throws IOException {
		writer.beginObject();
		if (this.headers != null) {
			writer.name("headers");
			this.headers.write(writer);
		}
		if (this.arguments != null) {
			writer.name("arguments");
			this.writerArguments(writer);
		}
		writer.endObject();
	}

	public void read(JsonReader reader) throws IOException {
		reader.beginObject();
		while (reader.hasNext()) {
			String name = reader.nextName();
			if ("headers".equals(name)) {
				RequestHeaders value = new RequestHeaders();
				value.read(reader);
				this.headers = value;
			} else if ("arguments".equals(name)) {
				this.arguments = this.readArguments(reader);
			} else {
				reader.skipValue();
			}
		}
		reader.endObject();
	}
	
	private void writerArguments(JsonWriter writer) throws IOException {
		writer.beginArray();
		for (String arg : this.arguments) {
			writer.value(arg);
		}
		writer.endArray();
	}

	private String[] readArguments(JsonReader reader) throws IOException {
		List<String> list = new ArrayList<String>();
		reader.beginArray();
		while (reader.hasNext()) {
			list.add(reader.nextString());
		}
		reader.endArray();
		
		return list.toArray(new String[0]);
	}
	
	public String toDebugString(String clientIp){
		StringBuilder sb = new StringBuilder();
		sb.append("agent: ").append(headers.getAgent())
			.append(", apiVersion: ").append(headers.getApiVersion())
			.append(", className: ").append(headers.getClazzName())
			.append(", methodName: ").append(headers.getMethodName())
			.append(", argument size is: ").append(arguments.length)
			.append(", dataFormat: ").append(headers.getDataFormat())			
			.append(", sessionId: ").append(headers.getSessionId())
			.append(", clientIp: ").append(clientIp);
		return sb.toString();
	}
}
